package com.goldmantis.alitrip.service;

import com.goldmantis.alitrip.common.BaseService;
import com.goldmantis.alitrip.common.SysLogUtil;
import com.goldmantis.alitrip.common.SysTreeModel;
import com.goldmantis.alitrip.common.sysenum.EnumLogCatagory;
import com.goldmantis.alitrip.common.sysenum.EnumOperationType;
import com.goldmantis.alitrip.mapper.SysResMapper;
import com.goldmantis.alitrip.mapper.SysRoleMapper;
import com.goldmantis.alitrip.mapper.SysRoleResMapper;
import com.goldmantis.alitrip.pojo.SysLog;
import com.goldmantis.alitrip.pojo.SysRes;
import com.goldmantis.alitrip.pojo.SysRole;
import com.goldmantis.alitrip.pojo.SysRoleRes;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Transformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 9:46 2018/2/11
 */
@Service
public class SysRoleResService extends BaseService<SysRoleRes> {

    @Autowired
    private SysRoleResMapper sysRoleResMapper;

    @Autowired
    private  SysResMapper sysResMapper;

    @Autowired
    private SysLogService sysLogService;


    public void insertBatch(SysRoleRes sysRoleRes){

        Example example=new Example(SysRoleRes.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleid",sysRoleRes.getRoleid());

        //记录原先数据
        List<SysRoleRes> roleResOrginal = this.sysRoleResMapper.selectByExample(example);

        List<Long> newItems=new ArrayList<>();
        if(sysRoleRes.getResIds()!=null&&sysRoleRes.getResIds().size()>0){

            for(Long resId : sysRoleRes.getResIds()){

                //防止重复添加
                boolean flag=false;
                for (SysRoleRes itemOriginal :roleResOrginal) {
                    if(itemOriginal.getResid().longValue()==resId.longValue()){
                        flag=true;
                        break;
                    }
                }

                if(!flag) {
                    Long systemid =0L;
                    SysRes sysRes = this.sysResMapper.selectByPrimaryKey(resId);
                    if(sysRes != null){
                        systemid = sysRes.getSystemid();
                    }

                    SysRoleRes item = new SysRoleRes();
                    item.setResid(resId);
                    item.setRoleid(sysRoleRes.getRoleid());
                    item.setSystemid(systemid);
                    super.insert(item);


                    SysLog baseSysLog = SysLogUtil.getBaseSysLog();
                    baseSysLog.setCategoryId(EnumLogCatagory.operation.getValue());
                    baseSysLog.setDescription("角色分配菜单");
                    baseSysLog.setOperateTypeId(String.valueOf(EnumOperationType.grant.getValue()));
                    baseSysLog.setOperateType(EnumOperationType.grant.getText());
                    baseSysLog.setSourceObjectId(String.valueOf(resId));


                    sysLogService.insert(baseSysLog);
                }
                newItems.add(resId);

            }
        }

        //删除掉以前有、现在没有的数据
        for(SysRoleRes itemOriginal :roleResOrginal){
            //防止重复添加
            boolean flag=false;
            for (Long itemNew :newItems) {
                if(itemOriginal.getResid().longValue()==itemNew.longValue()){
                    flag=true;
                    break;
                }
            }

            if(!flag) {
                this.sysRoleResMapper.delete(itemOriginal);

                SysLog baseSysLog = SysLogUtil.getBaseSysLog();
                baseSysLog.setCategoryId(EnumLogCatagory.operation.getValue());
                baseSysLog.setDescription("角色撤销菜单");
                baseSysLog.setOperateTypeId(String.valueOf(EnumOperationType.revoke.getValue()));
                baseSysLog.setOperateType(EnumOperationType.revoke.getText());
                baseSysLog.setSourceObjectId(String.valueOf(itemOriginal.getResid()));


                sysLogService.insert(baseSysLog);
            }
        }

    }


    public List<Long> selectByRoleId(Long roleId) {

        SysRoleRes sysRoleRes=new SysRoleRes();
        sysRoleRes.setRoleid(roleId);
        List<SysRoleRes> sysRoleReses = this.sysRoleResMapper.select(sysRoleRes);

        List<Long> list=new ArrayList<>();
        CollectionUtils.collect(sysRoleReses, new Transformer<SysRoleRes,Long>() {
            @Override
            public Long transform(SysRoleRes input) {
                return input.getResid();
            }
        },list);

        return list;

    }

    public List<Map> selectByResId(Long resId) {
        return this.sysRoleResMapper.selectByResId(resId);
    }

}
